package org.dragonnova.meetingserver.model.bean;

import android.net.Uri;

import com.google.gson.GsonBuilder;

import org.dragonnova.meetingserver.utils.Constants;
import org.dragonnova.meetingserver.utils.LogUtil;

import java.io.Serializable;
import java.net.SocketAddress;
import java.util.Arrays;
import java.util.Objects;

/**
 * 语音数据实体
 * Created by Administrator on 2015/7/29 15:45.
 * Email: thinkdata@163.com
 */
public class VoiceInfo implements Serializable {

    public static final String CONTENT = "voice";
    public final static Uri CONTENT_VOICE_URI = Uri.parse("content://" + Constants.AUTHORITY + "/" + CONTENT);

    public final static String KEY_ID_COLUMN = "_id";
    public static final String KEY_ORG_TRAN_TEXT_COLUMN = "ORG_TEXT";
    public static final String KEY_ORG_PATH_COLUMN = "ORG_PATH";
    public static final String KEY_TRAN_TEXT_COLUMN = "TRAN_TEXT";
    public static final String KEY_INSERT_DATE_COLUMN = "INSERT_DATE";
    public static final String KEY_VOICE_TYPE_COLUMN = "VOICE_LANGUAGE";
    public static final String KEY_VOICE_ORG_COST_TIME_COLUMN = "VOICE_COST_TIME";
    public static final String KEY_VOICE_USER_ID_FOREIGN_COLUMN = "USER_ID";
    public static final String KEY_VOICE_FLAG_COLUMN = "FLAG";


    private final static long serialVersionUID = 1L;
    /**
     * 序列
     */
    private long id;
    /**
     * 原始语音数据
     */
    private byte[] orgData;
    /**
     * 合成语音
     */
    private byte[] tranData;
    /**
     * 原始文字描述
     */
    private String orgContent;
    /**
     * 翻译文字描述
     */
    private String tranContent;
    /**
     * 插入时间
     */
    private long insertTime;
    /**
     * 语音类型，e.英文 c.中文
     */
    private char type;
    /**
     * 原始语音路径
     */
    private String orgPath;
    /**
     * 原始包地址
     */
    private SocketAddress orgAddress;
    /**
     * 原始语音录音时长
     */
    private int orgCostTime;
    /**
     * 用户信息，暂用名称
     */
    private String userId;
    /**
     * 删除标志位
     */
    private int flag;


    public VoiceInfo(long id, long insertTime, char type, String orgContent) {
        this.id = id;
        this.insertTime = insertTime;
        this.type = type;
        this.orgContent = orgContent;
    }

    public VoiceInfo(long insertTime, char type, String orgContent) {
        this.insertTime = insertTime;
        this.type = type;
        this.orgContent = orgContent;
    }

    public VoiceInfo(long insertTime, char type, String orgContent, String orgPath) {
        this.insertTime = insertTime;
        this.type = type;
        this.orgContent = orgContent;
        this.orgPath = orgPath;
    }

    public VoiceInfo(String orgContent, String tranContent, long insertTime, char type, String orgPath, int orgCostTime, String userId, int flag) {
        this.orgContent = orgContent;
        this.tranContent = tranContent;
        this.insertTime = insertTime;
        this.type = type;
        this.orgPath = orgPath;
        this.orgCostTime = orgCostTime;
        this.userId = userId;
        this.flag = flag;
    }

    public VoiceInfo(long id, String orgContent, String tranContent, long insertTime, char type, String orgPath, int orgCostTime, String userId, int flag) {
        this.id = id;
        this.orgContent = orgContent;
        this.tranContent = tranContent;
        this.insertTime = insertTime;
        this.type = type;
        this.orgPath = orgPath;
        this.orgCostTime = orgCostTime;
        this.userId = userId;
        this.flag = flag;
    }

    /**
     * 原始识别文字
     *
     * @param insertTime
     * @param type
     * @param data
     */
    public VoiceInfo(long insertTime, char type, byte[] data) {
        this.insertTime = insertTime;
        this.type = type;
        this.orgContent = new String(data);
        this.orgContent.replaceAll("/", "").trim();
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof VoiceInfo)) return false;
        VoiceInfo voiceInfo = (VoiceInfo) o;
        return Objects.equals(getId(), voiceInfo.getId()) &&
                Objects.equals(getType(), voiceInfo.getType());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getId(), getType());
    }

    public int getOrgCostTime() {
        return orgCostTime;
    }

    public void setOrgCostTime(int orgCostTime) {
        this.orgCostTime = orgCostTime;
    }

    public SocketAddress getOrgAddress() {
        return orgAddress;
    }

    public void setOrgAddress(SocketAddress orgAddress) {
        this.orgAddress = orgAddress;
    }

    public String getOrgPath() {
        return orgPath;
    }

    public void setOrgPath(String orgPath) {
        this.orgPath = orgPath;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public byte[] getOrgData() {
        return orgData;
    }

    public void setOrgData(byte[] orgData) {
        this.orgData = orgData;
    }

    public byte[] getTranData() {
        return tranData;
    }

    public void setTranData(byte[] tranData) {
        this.tranData = tranData;
    }

    public String getOrgContent() {
        return orgContent;
    }

    public void setOrgContent(String orgContent) {
        this.orgContent = orgContent;
    }

    public String getTranContent() {
        return tranContent;
    }

    public void setTranContent(String tranContent) {
        this.tranContent = tranContent;
    }

    public long getInsertTime() {
        return insertTime;
    }

    public void setInsertTime(long insertTime) {
        this.insertTime = insertTime;
    }

    public char getType() {
        return type;
    }

    public void setType(char type) {
        this.type = type;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public int getFlag() {
        return flag;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }

    public byte[] encode() {
        String msg = new GsonBuilder().create().toJson(this);
        return msg.getBytes();
    }

    public static VoiceInfo parse(String json) {
        LogUtil.eee(json);
        return new GsonBuilder().create().fromJson(json, VoiceInfo.class);
    }

    @Override
    public String toString() {

        return "VoiceInfo{" +
                "id=" + id +
                ", orgData=" + Arrays.toString(orgData) +
                ", tranData=" + Arrays.toString(tranData) +
                ", orgContent='" + orgContent + '\'' +
                ", tranContent='" + tranContent + '\'' +
                ", insertTime=" + insertTime +
                ", type=" + type +
                ", orgPath='" + orgPath + '\'' +
                ", orgAddress=" + orgAddress +
                ", orgCostTime=" + orgCostTime +
                ", userId='" + userId + '\'' +
                ", flag=" + flag +
                '}';
    }
}
